package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void addAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insert(addressBook);
    }

    @Override
    public List<AddressBook> selectAllAddressBook(AddressBook addressBook) {

       List<AddressBook>list= addressBookMapper.select(addressBook);
       return list;

    }

    @Override
    public AddressBook getById(Long id) {
    AddressBook addressBook=    addressBookMapper.selectById(id);
        return addressBook;
    }

    @Override
    public void update(AddressBook addressBook) {
      addressBook.setUserId(BaseContext.getCurrentId());
                addressBookMapper.update(addressBook);
    }

    @Override
    public void delete(Long id) {
         addressBookMapper.delete(id);
    }

    @Override
    public void defult(AddressBook addressBook) {

        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);

        addressBook.setIsDefault(1);
        addressBookMapper.updateDeafult(addressBook);
    }

    @Override
    public AddressBook getDefault() {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
      addressBookMapper.selectDefault(addressBook);
        return addressBook;
    }
}
